<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="30vw" :title="`${drawerProps.title}评论`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <!-- <el-form-item label="回答图片" prop="fileList">
        <UploadImgs v-model:file-list="drawerProps.row!.fileList" height="140px" width="140px">
          <template #empty>
            <el-icon><Picture /></el-icon>
            <span>请上传照片</span>
          </template>
          <template #tip> 照片大小不能超过 5M </template>
        </UploadImgs>
      </el-form-item> -->
      <!-- <el-form-item label="分类" prop="typeId">
        <el-cascader
          v-model="drawerProps.row!.typeId"
          :options="options"
          placeholder="请选择分类"
          clearable
          :props="defaultOptions"
          ref="cascaderRef"
        />
      </el-form-item> -->
      <!-- <el-form-item label="标题" prop="title">
        <el-input v-model="drawerProps.row!.title" placeholder="请填写标题" clearable></el-input>
      </el-form-item> -->
      <el-form-item label="内容" prop="content">
        <el-input class="at-h" type="textarea" v-model="drawerProps.row!.content" placeholder="请填写内容" clearable></el-input>
      </el-form-item>
      <el-form-item v-show="drawerProps.isView" label="点赞数" prop="likeNumber">
        <el-input v-model="drawerProps.row!.likeNumber" placeholder="请填写点赞数" clearable></el-input>
      </el-form-item>
      <!-- <el-form-item v-show="drawerProps.isView" label="评论数" prop="commentNumber">
        <el-input v-model="drawerProps.row!.commentNumber" placeholder="请填写评论数" clearable></el-input>
      </el-form-item> -->
      <el-form-item label="备注" prop="remark">
        <el-input v-model="drawerProps.row!.remark" placeholder="请填写备注" clearable></el-input>
      </el-form-item>
      <!-- <el-form-item v-show="drawerProps.isView" label="修改人" prop="modifiedBy">
        <el-input v-model="drawerProps.row!.modifiedBy" placeholder="请填写备注" clearable></el-input>
      </el-form-item>
      <el-form-item v-show="drawerProps.isView" label="修改时间" prop="gmtModified">
        <el-input v-model="drawerProps.row!.gmtModified" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item v-show="drawerProps.isView" label="创建人" prop="createBy">
        <el-input v-model="drawerProps.row!.createBy" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item v-show="drawerProps.isView" label="创建时间" prop="gmtCreate">
        <el-input v-model="drawerProps.row!.gmtCreate" placeholder="" clearable></el-input>
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false"> 取消 </el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive, onMounted } from "vue";
// import { genderType } from "@/utils/serviceDict";
import { ElMessage, FormInstance } from "element-plus";
import { User, File } from "@/api/interface";
// import UploadImgs from "@/components/Upload/Imgs.vue";
// import {getParamTreeByChildId } from "@/api/modules/sys";
let defaultOptions = ref();
const rules = reactive({
  fileList: [{ required: true, message: "请上传图片" }],
  content: [{ required: true, message: "请填写内容" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<User.ResUserList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  getDict: (params: any) => Promise<any>;
  fileList: File.ResFileList[];
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {
    fileList: []
  }
});

// 接收父组件传过来的参数
const acceptParams = async (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
  // 保存
  defaultOptions.value = {
    lazy: true,
    label: "name",
    value: "id",
    checkStrictly: true
  };
  // if (params.row.typeId) {
  //   let result = await getParamTreeByChildId({ childId: params.row.typeId });
  //   drawerProps.value.row.typeId = result.data.splice(1);
  // }
};
// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      // const answerImgModels = drawerProps.value.row.fileList.map(item => {
      //   return {
      //     name: item.originalFileName,
      //     path: item.path,
      //     answerId: drawerProps.value.row.id
      //   };
      // });
      // drawerProps.value.row.answerImgModels = answerImgModels;
      // drawerProps.value.row.typeId = drawerProps.value.row.typeId[drawerProps.value.row.typeId.length - 1];
      delete drawerProps.value.row.fileList;
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}评论成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};
onMounted(async () => {
  // const { data } = await getDict!({ parentId: dict.questionId });
  // options.value = data;
});
defineExpose({
  acceptParams
});
</script>
<style scoped="sass">
.el-textarea__inner {
  height: 80px;
}
</style>
